Read a random line from a fileΒΆ

Read a random line from a file.
import random

def random_line(fname):
    lines = open(fname).read().splitlines()
    return random.choice(lines)

print(random_line('test.txt'))

Output:

Append this text.